package View;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class UserVehicleWindow {

    private JFrame frame = new JFrame("车辆浏览和租车管理");
    private JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    private JPanel browsePanel = new JPanel();
    private JPanel rentalPanel = new JPanel();

    private JTable browseTable = new JTable();
    private JTable rentalTable = new JTable();
    private JButton returnButton = new JButton("还车");

    private JButton rentButton = new JButton("还车");
    private JLabel lblCarName =new JLabel("车辆名称:");
    private JTextField vehicleSearchTextField = new JTextField(10);
    private JButton vehicleSearchButton = new JButton("查询车辆");

    public UserVehicleWindow(UserMain_window userMainWindow) {
        frameControl(userMainWindow);
    }

    // 设置容器管理
    public void frameControl(UserMain_window userMainWindow) {
        // 设置底层容器和布局管理器
        frame.setSize(600, 400);
        frame.setLocation(500, 300);
        frame.setResizable(false);

        // 设置车辆浏览面板
        JScrollPane browseScrollPane = new JScrollPane(browseTable);
        browsePanel.setLayout(new BorderLayout());
        JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        searchPanel.add(lblCarName,FlowLayout.LEFT);
        searchPanel.add(vehicleSearchTextField,FlowLayout.CENTER);
        searchPanel.add(vehicleSearchButton,FlowLayout.RIGHT);
        browsePanel.add(searchPanel, BorderLayout.NORTH);
        browsePanel.add(browseScrollPane, BorderLayout.CENTER);

        // 设置租车管理面板
        JScrollPane rentalScrollPane = new JScrollPane(rentalTable);
        rentalPanel.setLayout(new BorderLayout());
        rentalPanel.add(rentalScrollPane, BorderLayout.CENTER);
        rentalPanel.add(rentButton, BorderLayout.SOUTH);

        // 添加标签页
        tabbedPane.addTab("车辆浏览", null, browsePanel, null);
        tabbedPane.addTab("租车管理", null, rentalPanel, null);

        // 设置整体布局
        frame.setLayout(new BorderLayout());
        frame.add(tabbedPane, BorderLayout.CENTER);

        // 设置按钮事件处理
        setButtonActions(userMainWindow);

        // 设置窗口可见性和关闭操作
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 初始时获取车辆信息和个人租车信息
        getBrowseTableData();
        getRentalTableData();
    }

    // 设置按钮事件处理
    public void setButtonActions(UserMain_window userMainWindow) {
        rentButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 执行租车操作，可以添加租车逻辑
                JOptionPane.showMessageDialog(null, "租车成功！");
                // 刷新租车管理面板的信息
                getRentalTableData();
            }
        });

        vehicleSearchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 执行查询车辆操作
                String searchText = vehicleSearchTextField.getText();
                if (searchText.equals("")) {
                    // 如果搜索框为空，显示所有车辆信息
                    getBrowseTableData();
                } else {
                    // 否则，执行根据条件查询车辆信息
                    findVehicle(searchText);
                }
            }
        });
    }

    // 获取车辆信息
    private void getBrowseTableData() {
        // 模拟获取车辆信息的数据
        String[] header = {"车辆ID", "车型", "品牌", "价格"};
        String[][] data = {
                {"1", "小轿车", "Toyota", "200"},
                {"2", "SUV", "Honda", "300"},
                {"3", "卡车", "Ford", "400"}
                // 可根据实际情况添加更多数据
        };
        browseTable.setModel(new DefaultTableModel(data, header));
    }

    // 查询车辆信息
    private void findVehicle(String searchText) {
        // 模拟查询车辆信息的数据
        String[] header = {"车辆ID", "车型", "品牌", "价格"};
        String[][] data = {
                {"1", "小轿车", "Toyota", "200"}
                // 根据实际情况添加查询到的数据
        };
        browseTable.setModel(new DefaultTableModel(data, header));
    }

    // 获取个人租车信息
    private void getRentalTableData() {
        // 模拟获取个人租车信息的数据
        String[] header = {"订单ID", "车辆ID", "车型", "租金"};
        String[][] data = {
                {"101", "1", "小轿车", "200"},
                {"101", "1", "小轿车", "200"},
                {"101", "1", "小轿车", "200"},
                {"101", "1", "小轿车", "200"},
                {"101", "1", "小轿车", "200"},
                {"101", "1", "小轿车", "200"},
                {"101", "1", "小轿车", "200"},
                {"101", "1", "小轿车", "200"},
                {"101", "1", "小轿车", "200"},
                {"101", "1", "小轿车", "200"},
                {"101", "1", "小轿车", "200"},
                {"101", "1", "小轿车", "200"},
                {"101", "1", "小轿车", "200"},
                {"101", "1", "小轿车", "200"},
                {"101", "1", "小轿车", "200"},
                {"101", "1", "小轿车", "200"},
                {"101", "1", "小轿车", "200"},
                {"101", "1", "小轿车", "200"},
                {"102", "2", "SUV", "300"}
                // 可根据实际情况添加更多数据
        };
        rentalTable.setModel(new DefaultTableModel(data, header));
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            UserVehicleWindow userVehicleWindow = new UserVehicleWindow(new UserMain_window());
        });
    }
}


